import random



def convert():
    '''根据QA与QB生成可选队员列表'''
    h = int(txt1.get()) #获取h个整数
    listbox1.delete(1, tk.END) #clear list
    txt2.delete(0, tk.END)
    txt3.delete(0, tk.END)
    listqa = [0] * 10
    listqb = [0] * 10
    listNum = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    j = 0


    for i in range(0, 10):
        qa = random.randint(1, 200)  #随机抽取一个数字QA
        qb = random.randint(1, 200)  # 随机抽取一个数字Qb
        if (qa > h and qb > h):
            if(abs(qa-qb) > h):
                     listqa[j] = qa
                     listqb[j] = qb
                     j=j+1
    print(listqb) #输出qb
    print(listqa) #输出qb
    #listqb.sort(reverse = True) #排序qb

    #找listqb最大值,并转换
    for i in range(10):
        indexNum = i
        for j in range(0, 10-i-1):
            if(listqb[j] < listqb[j+1]):
                listqb[j], listqb[j+1] = listqb[j+1], listqb[j]  #qb交换最大值
                listqa[j], listqa[j+1] = listqa[j+1], listqa[j]  # qb交换最大值
                listNum[j], listNum[j+1] = listNum[j+1], listNum[j] #同步交换编号
            # qb交换最大值

    print(listqb) #输出qb
    print(listqa) #输出qb
    for i in range(0, 10):
               listbox1.insert(tk.END, '    {:>4}      {:>4}      {:>4}'.format(listNum[i], listqa[i], listqb[i])) # 列表框输出

    txt2.insert(tk.END, listNum[0])#显示队员编号
    txt3.insert(tk.END, listNum[1])#显示队员编号


import tkinter as tk
# 建立窗口
win = tk.Tk()
win.title('选取队员（筛选10个200内随机数）')
win.resizable(0, 0)

# 建立组件
lb1 = tk.Label(win, text = '参数H（可改)')
lb2 = tk.Label(win, text = '编号数是:')

txt1 = tk.Entry(win)
txt1.insert(0, '12')
txt2 = tk.Entry(win)
txt2.insert(0, ' ')
txt3 = tk.Entry(win)
txt3.insert(0, ' ')

listbox1 = tk.Listbox(win,)
listbox1.insert(0,'  编号        QA          QB')
#cmd = tk.Button(win, text = '    点击执行    ')
cmd = tk.Button(win, text = '    点击执行(显示结果)    ', command = convert)

# 组件布局
lb1.grid(row=0, column=0)
lb2.grid(row=1, column=0)

txt1.grid(row=0, column=1)
txt2.grid(row=1, column=1)
txt3.grid(row=2, column=1)

cmd.grid(row=3, column=1)
listbox1.grid(row=0,column=2,rowspan=3)

# 接收窗口事件
win.mainloop()